/**
 * stools
 */

#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h> //size_t

#include "log.h"
#include "iniparser.h"
#include "zk_handle.h"

static void print_help() {
    const char * help = "Usage: stools [OPTION]\n"
                        "     -c    the path of php.ini\n"
                        "     -a    ation name, such as 'delete'\n"
                        ;
    printf("%s\n", help);
}

static int parse_ini_file(const char * ini_file_name, char * zk_addrs_buf, size_t zk_addrs_buf_len, char * log_path_buf, size_t log_path_buf_len)
{
    dictionary * ini;
    char * s;

    ini = iniparser_load(ini_file_name);
    if (ini == NULL) {
        fprintf(stderr, "cannot parse file: %s\n", ini_file_name);
        return -1 ;
    }
    // iniparser_dump(ini, stderr);

    s = iniparser_getstring(ini, "SCP:scp.zk_addrs", NULL);
    snprintf(zk_addrs_buf, zk_addrs_buf_len, "%s", s );

    s = iniparser_getstring(ini, "SCP:scp.log_path", NULL);
    snprintf(log_path_buf, log_path_buf_len, "%s", s );

    iniparser_freedict(ini);
    return 0 ;
}

int get_dir_path (const char * file_path, char * buf, size_t buf_len)
{
    char * ptr = (char *)file_path;
    int end_pos = strlen(file_path);
    int found_pos = -1;
    while (end_pos-- > 0)
    {
        if (ptr[end_pos] == '/')
        {
            found_pos = end_pos;
            break;
        }
    }
    if (found_pos == -1) return -1;
    memcpy(buf, file_path, found_pos);
    *(buf + found_pos) = '\0';
    return 0;
}

int read_content_from_file(const char * filename, char * buffer, size_t len) {
    if (filename == NULL || buffer == NULL || len == 0) {
        return -1;
    }
    FILE * file = fopen(filename, "r");
    if (!file) {
        return -1;
    }
    fseek(file, 0, SEEK_END);
    long size = ftell(file);
    if (len <= size) {
        size = len - 1;
    }
    fseek(file, 0, SEEK_SET);
    size_t ret = fread(buffer, size, 1, file);
    if (ret != 1) {
        fclose(file);
        return -1;
    }
    *(buffer + size) = '\0';
    fclose(file);
    return 0;
}

static void remove_node(const char * zk_addrs, const char * log_dir) {
    char node_info_file_path[PATH_MAX];
    snprintf(node_info_file_path, PATH_MAX, "%s/node",  log_dir);
    char node_info[4096];
    read_content_from_file(node_info_file_path, node_info, 4096);
    log_trace("node_info : [%s]", node_info);
    zk_handle zk;
    memset(&zk, 0, sizeof(zk_handle));
    zk.receive_timeout = 3000;
    init_zk_handle(&zk, zk_addrs);
    delete_node(&zk, node_info);
    fini_zk_handle(&zk);
}

int main(int argc, char *argv[])
{
    char * config_file_path = NULL;
    char * action = NULL;
    int disable_log = 0;
    int ch;
    opterr = 0;
    while ((ch = getopt(argc, argv, "c:a:d")) != -1) {
        switch (ch)
        {
        case 'c':
        {
            config_file_path = optarg;
            break;
        }
        case 'a':
        {
            action = optarg;
            break;
        }
        case 'd':
        {
            disable_log = 1;
            break;
        }
        default: {
        }
        }
    }
    if (!config_file_path || !action) {
        print_help();
        return 0;
    }
    if (disable_log) {
        set_log_level(LEVEL_NONE);
    }
    char zk_addrs_buf[256];
    char log_path_buf[PATH_MAX];
    char log_dir_buf[PATH_MAX];
    parse_ini_file(config_file_path, zk_addrs_buf, 256, log_path_buf, PATH_MAX);
    get_dir_path(log_path_buf, log_dir_buf, PATH_MAX);
    log_trace("zk_addrs_buf : [%s]", zk_addrs_buf);
    log_trace("log_dir_buf : [%s]", log_dir_buf);
    //
    if (strcmp(action, "delete") == 0) {
        remove_node(zk_addrs_buf, log_dir_buf);
    }
    return 0;
}
